<?php
namespace app\index\controller;

use app\admin\model\Offline;
use app\first\model\Article;
use app\first\model\Dream;
use think\Controller;
use think\Db;
use think\db\Where;

class Task extends Controller{
    public function index(){
        echo '欢迎使用Task';
    }

    /**
     * 下架资讯
     * @throws \think\Exception
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function offline_news(){
        $art = new Article();
        $where = new Where();
        $where['status'] = 2;
        $where['vip_type'] = ['in','0,1'];

        Db::startTrans();
        $list = $art->field('id')
            ->whereTime('check_time','yesterday')
            ->where($where)
            ->select();
        $_id = $_name = [];
        foreach ($list as $v){
            array_push($_id,$v['id']);
            array_push($_name,$v['title']);
        }
        $data = [
            'title'=> date('Y-m-d H:i:s').'执行下架操作',
            'ids'=>$_id?'':implode(',',$_id),
            'names'=>$_name?'':implode(',',$_name),
            'create_time'=>time()
        ];
        $result = $art
            ->whereIn('id',$_id)
            ->update(['status'=>4,'info'=>'显示到期系统下架','update_time'=>time()]);
        $offline = new Offline();
        if($list && $result) {
            Db::commit();
            $count = count($list);
            $msg = "本次执行成功,共下架了{$count}条数据";
            $data['msg'] = $msg;
            $offline->save($data);
            echo $msg;
        }else{
            Db::rollback();
            $msg = '本次执行成功，但是没有需要下架的内容';
            $data['msg'] = $msg;
            $offline->save($data);
            echo $msg;
        }
    }

    /**
     * 下架梦想加
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function offline_dream(){
        $art = new Dream();
        $where = new Where();
        $where['status'] = 2;
        $list = $art->where($where)->whereTime('check_time','-30 days')->select();
        $_id = $_name = [];
        foreach ($list as $v){
            array_push($_id,$v['id']);
        }
        $data = [
            'title'=> date('Y-m-d H:i:s').'执行下架操作',
            'ids'=>$_id?'':implode(',',$_id),
            'names'=>$_name?'':implode(',',$_name),
            'create_time'=>time()
        ];
        $result = $art
            ->whereIn('id',$_id)
            ->update(['status'=>4,'info'=>'显示到期系统下架','update_time'=>time()]);
        $offline = new Offline();
        if($list && $result) {
            Db::commit();
            $count = count($list);
            $msg = "本次执行成功,共下架了{$count}条数据";
            $data['msg'] = $msg;
            $offline->save($data);
            echo $msg;
        }else{
            $msg = '本次执行成功，但是没有需要下架的内容';
            $data['msg'] = $msg;
            $offline->save($data);
            echo $msg;
        }
    }
}